package com.zhangfb95.crosschain.console.command;

import com.zhangfb95.crosschain.console.enums.CommandEnum;
import com.zhangfb95.crosschain.infra.model.Result;
import com.zhangfb95.crosschain.sdk.CrossChainClient;
import com.zhangfb95.crosschain.sdk.infra.model.api.response.server.rpcuser.RpcUserLoginApiRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author zhangfubing
 * @since 2023/12/21
 */
@Component
public class LoginCommand implements Command {

    @Autowired
    private CrossChainClient crossChainClient;

    @Override
    public CommandEnum command() {
        return CommandEnum.LOGIN;
    }

    @Override
    public boolean handle(String[] args) {
        if (args.length < 2 || isHelp(args)) {
            System.out.println("Usage: login [username] [password]");
            return false;
        }

        String username = args[0];
        String password = args[1];
        Result<RpcUserLoginApiRes> loginApiRes = crossChainClient.login(username, password);
        if (loginApiRes.isSuccess()) {
            System.out.println("Result: success");
            System.out.println("username: " + username);
        } else {
            System.out.println("Result: " + loginApiRes.getMsg());
        }
        return false;
    }
}
